package com.wisely.xxboot;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wisely.xxboot.conf.AuthorSettings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.http.MediaType;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.net.UnknownHostException;

/**
 * ActiveQM
 * 需要重新继承接口等信息
 * 20170704
 */
//@Controller
//@SpringBootApplication //1 @SpringBootApplication 它是springboot的核心注解
//@EnableCaching //开启缓存
//public class XXBootApplication {



//activemq
@SpringBootApplication //1 @SpringBootApplication 它是springboot的核心注解
public class XXBootApplication implements CommandLineRunner{
    @Autowired
    JmsTemplate jmsTemplate;

    @Override
    public void run(String... args)throws Exception{
        jmsTemplate.send("in-destination", new Msg());
    }
    /**
     * 其中一种取properties文件的方式
     */
//    @Value("${xdoes.author}")
//    private String WebAuthor;
//    @Value("{xdoes.name}")
//    private String WebName;

    @Autowired
    private AuthorSettings authorSettings; //可以用@Autowired直接引入该配置
    @Autowired
    private HelloSerivce helloSerivce;


//    @RequestMapping("/")
//    String index() {
////        return "Hello SpringBoot~ the Web author is:" + WebAuthor + " and Web name is:" + WebName;
//        return "Hello! author name is "+ authorSettings.getName() + " and author age is "+ authorSettings.getAge()+"\r\n<br/> "+ helloSerivce.sayHello();
//    }

//    @RequestMapping("/")
//    public String index(Model model){
//        Person single = new Person("ABC-Study", 9);
//        List<Person> list = new ArrayList<Person>();
//        Person p1 = new Person("A-Once", 11);
//        Person p2 = new Person("B-Double", 22);
//        Person p3 = new Person("C_triple", 33);
//        list.add(p1);
//        list.add(p2);
//        list.add(p3);
//
//        model.addAttribute("singlePerson",single);
//        model.addAttribute("people",list);
//
//        return "index";
//
//    }

    @RequestMapping(value = "/sreach",produces = {MediaType.APPLICATION_JSON_VALUE})
    public @ResponseBody Person search(String personName){
        return new Person(personName,32,"aping");
    }

    public static void main(String[] args) { //2 main方法
        SpringApplication.run(XXBootApplication.class, args);
    }


    /**
     * redis配置
     */
    @Bean
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory)
            throws UnknownHostException {
        RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>();
        template.setConnectionFactory(redisConnectionFactory);

        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);

        template.setValueSerializer(jackson2JsonRedisSerializer); //1
        template.setKeySerializer(new StringRedisSerializer()); //2

        template.afterPropertiesSet();
        return template;
    }


    /**
     * ActiveMQ
     */


    /**
     * 和CustomServletContainer.java以一样都实现了
     * 1.配置端口号为8888，
     * 2配置错误页面，根据httpstatus错误状态，直接跳向错误页面，404.html 500.html
     * 3配置session会话时间
     */
//    @Component
//    public static class CustomServletContainer implements EmbeddedServletContainerCustomizer{
//        @Override
//        public void customize(ConfigurableEmbeddedServletContainer var1){
//            var1.setPort(8888);
//            var1.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND,"/404.html"));
//            var1.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR,"/500.html"));
//            var1.setSessionTimeout(10, TimeUnit.MINUTES);
//        }
//    }
}
//8.2以前


/**
 * 例子8.2 使用的是restcontroller
 */
//import com.wisely.xxboot.dao.PersonRepository;
//import com.wisely.xxboot.support.CustomRepositoryFactoryBean;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.SpringApplication;
//import org.springframework.boot.autoconfigure.SpringBootApplication;
//import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
//
//@SpringBootApplication
//@EnableJpaRepositories(repositoryFactoryBeanClass = CustomRepositoryFactoryBean.class)
//public class XXBootApplication {
//    @Autowired
//    PersonRepository personRepository;
//
//
//    public static void main(String[] args) {
//        SpringApplication.run(XXBootApplication.class, args);
//
//    }
//
//
//}
